// pages/order/order-list/order-list.js
const app = getApp()
const constant = require("../../../constant.js")
const storage = require("../../../utils/storage.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabsActive: 0,
    empty: true,
    qiniu: constant.resource.qiniu
  },

  toggleActive: function(e) {
    
    this.setData({
      tabsActive: e.currentTarget.dataset.tabId
    })

    this.getOrderData();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      qiniu: constant.resource.qiniu
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.getOrderData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  getOrderData: function() {
    var that = this;
    let unionid = wx.getStorageSync(storage.keys.unionid);
    var paymentStatus = (that.data.tabsActive == null || that.data.tabsActive == '') ? 'unpaid' : that.data.tabsActive == '0' ? 'unpaid' : 'paid';

    wx.request({
      url: constant.api(app.globalData.pord).url + '/orders?unionid=' + unionid + '&paymentStatus=' + paymentStatus,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          var isEmpty = res.data.data.orders.length == 0 ? true : false;
          that.setData({
            user: res.data.data.user,
            orders: res.data.data.orders,
            tabsActive: res.data.data.paymentStatus,
            empty: isEmpty
          });
        }
      },
      fail: function (res) { }
    })
  }
})